package com.study.d012Components.listview;

import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.ListView.EditEvent;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
 * 通过复下拉框的方式修改ListView
 *
 * @description
 * @author xtayhame date 2020年3月13日 version since v1.0
 */
public class ListViewWithComboBoxListCell extends Application {
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		ObservableList<String> datas=FXCollections.observableArrayList();
		datas.addAll("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w");


		ObservableList<String> comboBoxList = FXCollections.observableArrayList();
		comboBoxList.add("长城");
		comboBoxList.add("故宫");
		comboBoxList.add("颐和园");


		ListView<String> listView = new ListView<String>(datas);
		listView.setPrefSize(200, 150);

		Callback<ListView<String>,ListCell<String>> callback2=ComboBoxListCell.forListView(new StringConverter<String>(){
			@Override
			public String toString(String object) {

				return object;
			}

			@Override
			public String fromString(String string) {
				// TODO Auto-generated method stub
				return string;
			}

		}, comboBoxList);

		//一定要可编辑才可以
		listView.setEditable(true);
		listView.setCellFactory(callback2);

		anchorPane.getChildren().add(listView);
		AnchorPane.setTopAnchor(listView, 10.0);
		AnchorPane.setLeftAnchor(listView, 20.0);
		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();

	}
}
